import base as base
import data_init as di
import genetic as ga
from pylab import array, log, zeros
import numpy
import datetime
import output as out
##Setting up Environment
ndt = 25 #number of time period
n = 20   #number of Airports
m = 20   #number of Flights
D = 5    #departure capacity of airport per time period
R = 2    #arrival capacity of airport per time period

dt = 15  #time period
A = 1    #upper limit on airborne delay in number of time period
G = 2    #upper limit on ground delay in number of time period
count = 0


[Airports, Flights] = di.RandomGeneration(ndt, n, m, D, R)
s = base.Solver(ndt, Airports, Flights, A=A, G=G, dt=dt, path='../data/data_20')
s._setup()
out.make_initial_schedule(Flights)

constraint_funcs = []
constraint_funcs.append(s.c_airport_capacity)
constraint_funcs.append(s.c_flight_)
constraint_funcs.append(s.c_connecting_flight)
constraint_funcs.append(s.c_on_a)



count = 0 
##Setting up optimizer

MINIMIZE = 11
MAXIMIZE = 12
count_t = 0 

  
    
class OneMax(ga.Individual):
    max_ind_tau_g = s.G       
    max_ind_tau_a = s.A + s.G 
    length = (max_ind_tau_g + max_ind_tau_a)*m 
    optimization = MAXIMIZE
    
    def evaluate(self, optimum=None,max_ind_tau_a=max_ind_tau_a,max_ind_tau_g=max_ind_tau_g):
        c = array(self.chromosome)
        c = c.reshape(m, max_ind_tau_g + max_ind_tau_a)
        self.score = 0
        ind_tau_a = zeros(len(s.Flights))
        ind_tau_g = zeros(len(s.Flights))
        for i in range(max_ind_tau_g):
            ind_tau_g += c[:,i]
        for i in range(max_ind_tau_a):
            ind_tau_a += c[:,max_ind_tau_g + i]
            
        
        s._update_ag(ind_tau_a,ind_tau_g)
        s._update_uv()
        
        constraint_satisfy = True
        

        for i in constraint_funcs:
            tmp = i()
            if tmp[0] == False:
                constraint_satisfy = False
                self.score -= tmp[1]
            else:
                pass
        if constraint_satisfy:
            self.score -= s.objective()
        else:
            self.score -= s.objective()*1000
   
    def mutate(self, gene):
       
        self.chromosome[gene] = not self.chromosome[gene] # bit flip

env = ga.Environment(OneMax, maxgenerations=1000, optimum=None,s=s)
env.run()
   

